package com.panshi.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@ApiModel("公共返回对象")
@Data
public class R {

    @ApiModelProperty(value = "是否成功")
    private boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private Map<String ,Object>  data= new HashMap<>();


    private R(){}

    //成功
    public static R ok(){
        R result = new R();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMsg("成功");
        return result;
    }

    //失败
    public static R error(){
        R result = new R();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR);
        result.setMsg("失败");
        return result;
    }

    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R msg(String msg){
        this.setMsg(msg);
        return this;
    }

    public R data(String key , Object value){
        this.data.put(key,value);
        return this;
    }

    public R data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

    //成功
    public static R ok(CodeMsgConstant codeMsgConstant){
        R result = new R();
        result.setSuccess(true);
        result.setCode(Integer.parseInt(codeMsgConstant.getCode()));
        result.setMsg(codeMsgConstant.getMsg());
        return result;
    }

    //失败
    public static R error(CodeMsgConstant codeMsgConstant){
        R result = new R();
        result.setSuccess(false);
        result.setCode(Integer.parseInt(codeMsgConstant.getCode()));
        result.setMsg(codeMsgConstant.getMsg());
        return result;
    }

}
